package com.zx._01_基础._019_servlet;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 点击登录按钮时，创建新的session
 *
 * @author 张雪
 */
public class HttpSessionServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");

        HttpSession session = request.getSession();
        session.setAttribute("name", name);
        session.setAttribute("password", password);
        session.setMaxInactiveInterval(15 * 60);//一般不用设置,服务器默认15~20分钟
        request.getRequestDispatcher("true.jsp").forward(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

